﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ePioneer_Reservation
    {
        readonly LYM.CommonWeb.DataServer.DataBase data = new LYM.CommonWeb.DataServer.DataBase(LYM.CommonWeb.ConfigHelper.GetConfigString("EMPC"), false);
        public ePioneer_Reservation()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ePioneer_Reservation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ePioneer_Reservation(");
            strSql.Append("UserId,CourseInfoId,CreateDate,Score,IsClose,Code)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@CourseInfoId,@CreateDate,@Score,@IsClose,@Code)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId",model.UserId),
					new SqlParameter("@CourseInfoId",model.CourseInfoId),
					new SqlParameter("@CreateDate",model.CreateDate),
					new SqlParameter("@Score",model.Score),
					new SqlParameter("@IsClose",model.IsClose),
                    new SqlParameter("@Code",model.Code)};

            object obj = null;
            data.GetValue(strSql.ToString(), false, ref obj, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_Reservation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ePioneer_Reservation set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("CourseInfoId=@CourseInfoId,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Score=@Score,");
            strSql.Append("IsClose=@IsClose,");
            strSql.Append("Code=@Code");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id",model.Id),
					new SqlParameter("@UserId",model.UserId),
					new SqlParameter("@CourseInfoId",model.CourseInfoId),
					new SqlParameter("@CreateDate",model.CreateDate),
					new SqlParameter("@Score",model.Score),
					new SqlParameter("@IsClose",model.IsClose),
                    new SqlParameter("@Code",model.Code)};

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false, ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ePioneer_Reservation ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false, ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ePioneer_Reservation ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false, ref rows, null);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_Reservation GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from ePioneer_Reservation ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            Model.ePioneer_Reservation model = new Model.ePioneer_Reservation();
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false, ref ds, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"] != null && ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = ds.Tables[0].Rows[0]["UserId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CourseInfoId"] != null && ds.Tables[0].Rows[0]["CourseInfoId"].ToString() != "")
                {
                    model.CourseInfoId = int.Parse(ds.Tables[0].Rows[0]["CourseInfoId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateDate"] != null && ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Score"] != null && ds.Tables[0].Rows[0]["Score"].ToString() != "")
                {
                    model.Score = int.Parse(ds.Tables[0].Rows[0]["Score"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsClose"] != null && ds.Tables[0].Rows[0]["IsClose"].ToString() != "")
                {
                    model.IsClose = int.Parse(ds.Tables[0].Rows[0]["IsClose"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Code"] != null && ds.Tables[0].Rows[0]["Code"].ToString() != "")
                {
                    model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ePioneer_Reservation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false,ref ds, null);
            return ds;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM ePioneer_Reservation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false,ref ds, null);
            return ds;
        }

        #endregion  Method
    }
}
